/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.web.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author tengyu
 */
public class DeviceCommand {

    public static String getSetCommand(String model, int deviceId, String key, String value) {
        String command = null;
        model = model.toUpperCase();
        if (model.indexOf("DT4-2") != -1) {
        } else if (model.indexOf("DT3") != -1) {
        }
        return command;
    }

    //type =0 读命令；type=1写命令
    public static Map<String, Object> excuteCommand(String server, String port, List<String> commandList, int type) {
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            socket = new Socket(server, Integer.parseInt(port));
            socket.setSoTimeout(30000);//超时30秒
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            result.put("success", false);
            result.put("message", "无法连接服务器" + e.getMessage());
            return result;
        } catch (IOException e) {
            result.put("success", false);
            result.put("message", "无法连接服务器2" + e.getMessage());
            return result;
        }
        try {
            int i = 0;
            for (String command : commandList) {
                out.println(command);
                Thread.sleep(1000);
                if (type == 0) {
                    String response = in.readLine();
                    result.put("response" + i, response);
                } else {
                    result.put("message","命令发送成功");
                }
                i++;
            }
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return result;
        } finally {
            out.close();
            try {
                in.close();
                socket.close();
            } catch (IOException ioe) {
            }
        }
        return result;
    }
    //TYPE=GetSXX^PARAM1=^SXZ=10280F^XXZ=00190F^PARAM2=0F8907000000^

    public static String deviceResponse2Json(String responseStr) {
        if (responseStr == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        String items[] = responseStr.split("\\^");
        for (String item : items) {
            String[] subItems = item.split("=");
            String key = subItems[0];
            String value;
            if (subItems.length > 1) {
                value = subItems[1];
            } else {
                value = "";
            }
            sb.append(key).append(":\"").append(value).append("\"");
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("}");
        return sb.toString();
    }
}
